import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calculator extends JFrame {
    private JTextField display;
    private boolean first=true;
    private double oldnum=0.0,newnum=0.0;
    private String op="";
    public Calculator() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        display = new JTextField();
        display.setHorizontalAlignment(SwingConstants.RIGHT);
        display.setFont(new Font("黑体", Font.PLAIN, 30));
        display.setText("0");
        display.setBounds(10, 10, 236, 38);
        getContentPane().add(display);
        display.setColumns(10);
        JButton btnNewButton = new JButton("1");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(btnNewButton.getText());
            }
        });
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
        btnNewButton.setBounds(10, 58, 52, 48);
        getContentPane().add(btnNewButton);

        JButton button = new JButton("2");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button.getText());
            }
        });
        button.setFont(new Font("宋体", Font.PLAIN, 20));
        button.setBounds(72, 58, 52, 48);
        getContentPane().add(button);

        JButton button_1 = new JButton("3");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_1.getText());
            }
        });
        button_1.setFont(new Font("宋体", Font.PLAIN, 20));
        button_1.setBounds(134, 58, 52, 48);
        getContentPane().add(button_1);
        JButton button_2 = new JButton("4");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_2.getText());
            }
        });
        button_2.setFont(new Font("宋体", Font.PLAIN, 20));
        button_2.setBounds(197, 58, 52, 48);
        getContentPane().add(button_2);
        JButton button_3 = new JButton("5");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_3.getText());
            }
        });
        button_3.setFont(new Font("宋体", Font.PLAIN, 20));
        button_3.setBounds(10, 116, 52, 48);
        getContentPane().add(button_3);

        JButton button_4 = new JButton("6");
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_4.getText());
            }
        });
        button_4.setFont(new Font("宋体", Font.PLAIN, 20));
        button_4.setBounds(72, 116, 52, 48);
        getContentPane().add(button_4);

        JButton button_5 = new JButton("7");
        button_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_5.getText());
            }
        });
        button_5.setFont(new Font("宋体", Font.PLAIN, 20));
        button_5.setBounds(134, 116, 52, 48);
        getContentPane().add(button_5);
        JButton button_6 = new JButton("8");
        button_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_6.getText());
            }
        });
        button_6.setFont(new Font("宋体", Font.PLAIN, 20));
        button_6.setBounds(197, 116, 52, 48);
        getContentPane().add(button_6);
        JButton button_7 = new JButton("9");
        button_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_7.getText());
            }
        });
        button_7.setFont(new Font("宋体", Font.PLAIN, 20));
        button_7.setBounds(10, 176, 52, 48);
        getContentPane().add(button_7);
        JButton button_8 = new JButton("0");
        button_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputNum(button_8.getText());
            }
        });
        button_8.setFont(new Font("宋体", Font.PLAIN, 20));
        button_8.setBounds(72, 174, 52, 48);
        getContentPane().add(button_8);

        JButton button_9 = new JButton("+");
        button_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputOP(button_9.getText());
            }
        });
        button_9.setFont(new Font("宋体", Font.PLAIN, 20));
        button_9.setBounds(134, 176, 52, 48);
        getContentPane().add(button_9);

        JButton button_10 = new JButton("-");
        button_10.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputOP(button_10.getText());
            }
        });
        button_10.setFont(new Font("宋体", Font.PLAIN, 20));
        button_10.setBounds(197, 174, 52, 48);
        getContentPane().add(button_10);
        JButton button_11 = new JButton("*");
        button_11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputOP(button_11.getText());
            }
        });
        button_11.setFont(new Font("宋体", Font.PLAIN, 20));
        button_11.setBounds(10, 234, 52, 48);
        getContentPane().add(button_11);
        JButton button_12 = new JButton("/");
        button_12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputOP(button_12.getText());
            }
        });
        button_12.setFont(new Font("宋体", Font.PLAIN, 20));
        button_12.setBounds(72, 234, 52, 48);
        getContentPane().add(button_12);
        JButton btnC = new JButton("C");
        btnC.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                first=true;
                oldnum=0.0;
                newnum=0.0;
                op="";
                display.setText("0");
            }
        });
        btnC.setFont(new Font("宋体", Font.PLAIN, 20));
        btnC.setBounds(134, 234, 52, 48);
        getContentPane().add(btnC);

        JButton button_14 = new JButton("=");
        button_14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                first=true;
                if(op.equals("")){
                    return;
                }
                switch(op.charAt(0)){
                    case '+':
                        display.setText(oldnum+newnum+"");
                        break;
                    case '-':
                        display.setText(oldnum-newnum+"");
                        break;
                    case '*':
                        display.setText(oldnum*newnum+"");
                        break;
                    case '/':
                        display.setText(oldnum/newnum+"");
                        break;
                }
                oldnum=Double.parseDouble(display.getText());
                //System.out.println(oldnum+" "+newnum);
            }
        });
        button_14.setFont(new Font("宋体", Font.PLAIN, 20));
        button_14.setBounds(197, 234, 52, 48);
        getContentPane().add(button_14);
        this.setSize(272, 332);
    }
    public void inputNum(String snum){
        if(first){
            oldnum=Double.parseDouble(display.getText());
            display.setText(snum);
            first=false;
        }else{
            display.setText(display.getText()+snum);
        }
        newnum=Double.parseDouble(display.getText());
        //System.out.println(oldnum+" "+newnum);
    }
    public void inputOP(String op){
        this.op=op;
        oldnum=Double.parseDouble(display.getText());
        first=true;
        //System.out.println(oldnum+" "+newnum);
    }
    public static void main(String[] args) {
        new Calculator().setVisible(true);
    }
}